package com.macauscholar.app

/**
 *  package : com.sgcc.xingfu
 *  author : zhiyi
 *  date : 30/09/2020 5:59 PM
 *  project : YDFirbaseSDKAndroid
 *  description :
 */
import android.util.Log
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage




class YDMessageService : FirebaseMessagingService()  {

    override fun onMessageSent(message: String) {
        super.onMessageSent(message)

        Log.i("wuyezhiguhun", ":<- YDMessageService ->: onMessageSent ->: message ->: $message")
    }

    override fun onDeletedMessages() {
        super.onDeletedMessages()


    }

    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        remoteMessage?.toIntent()?.extras.let {

            Log.i("wuyezhiguhun", ":<- YDMeessageService - onMessageReceived ->: Intent ->: extras ->: $it")

        }


        Log.i("wuyezhiguhun", ":<- YDMeessageService - onMessageReceived ********** ->:")

        // TODO(developer): Handle FCM messages here.
        // Not getting messages here? See why this may be: https://goo.gl/39bRNJ
        Log.d("wuyezhiguhun", "YDMessageService - From: ${remoteMessage?.from}")

        Log.i("wuyezhiguhun", ":<- YDMessageService ->: onMessageReceived ->: remoteMessage ->: $remoteMessage")
        Log.i("wuyezhiguhun", ":<- YDMessageService ->: onMessageReceived ->: remoteMessage from ->: " + remoteMessage?.from)

        // Check if message contains a data payload.
        remoteMessage?.data?.isNotEmpty()?.let {
            Log.i("wuyezhiguhun", "YDMessageService - Message data payload: " + remoteMessage.data)

            Log.i("wuyezhiguhun", ":<- YDMessageService ->: onMessageReceived ->: remoteMessage data ->: " + remoteMessage.data)

            if (/* Check if data needs to be processed by long running job */ true) {
                // For long-running tasks (10 seconds or more) use WorkManager.
                scheduleJob()
            } else {
                // Handle message within 10 seconds
                handleNow()
            }
        }

        // Check if message contains a notification payload.
        remoteMessage?.notification?.let {

            Log.i("wuyezhiguhun", ":<- YDMessageService ->: onMessageReceived ->: remoteMessage it ->: $it")

            Log.i("wuyezhiguhun", ":<- YDMessageService ->: onMessageReceived ->: remoteMessage body ->: " + it.body)

            Log.i("wuyezhiguhun", ":<- YDMessageService ->: onMessageReceived ->: remoteMessage body ->: $remoteMessage")

            Log.i("wuyezhiguhun", "YDMessageService - Message Notification Body: ${it.body}")
        }

        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated. See sendNotification method below.
    }
    // [END receive_message]

    // [START on_new_token]
    /**
     * Called if InstanceID token is updated. This may occur if the security of
     * the previous token had been compromised. Note that this is called when the InstanceID token
     * is initially generated so this is where you would retrieve the token.
     */
    override fun onNewToken(token: String) {
        Log.i("wuyezhiguhun", "YDMessageService - Refreshed token: $token")

        // If you want to send messages to this application instance or
        // manage this apps subscriptions on the server side, send the
        // Instance ID token to your app server.
        sendRegistrationToServer(token)
    }
    // [END on_new_token]

    /**
     * Schedule async work using WorkManager.
     */
    private fun scheduleJob() {
        Log.i("wuyezhiguhun", " :<- YDMessageService - scheduleJob ->: ")
    }

    /**
     * Handle time allotted to BroadcastReceivers.
     */
    private fun handleNow() {
        Log.i("wuyezhiguhun", "YDMessageService - Short lived task is done.")
    }

    /**
     * Persist token to third-party servers.
     *
     * Modify this method to associate the user's FCM InstanceID token with any server-side account
     * maintained by your application.
     *
     * @param token The new token.
     */
    private fun sendRegistrationToServer(token: String?) {
        Log.i("wuyezhiguhun", ":<- YDMessageService - sendRegistrationToServer ->: $token")
    }

}